package ${package};

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hxkj.demo.common.controller.BaseController;
import com.hxkj.demo.common.util.R;
import ${basePackage}.model.${entity.name};
import ${basePackage}.controller.query.${entity.name}Query;
import ${basePackage}.service.${entity.name}Service;

/**
 * ${entity.displayName} 接口
 */
\@Controller
public class ${entity.name}Controller  extends BaseController{

    private static final String MODEL = "/${target.urlBase}/${entity.code}";

    @var service=entity.code+"Service";

    \@Autowired private ${entity.name}Service ${service};
    
    /* 页面 */

    \@GetMapping(MODEL + "/index.do")
    \@RequiresPermissions("${basicfunctionCode}:query")
    public String index(Model model) {
        model.addAttribute("search", ${entity.name}Query.class.getName());
        return "/${target.urlBase}/${entity.code}/index";
    }

    \@GetMapping(MODEL + "/edit.do")
    \@RequiresPermissions("${basicfunctionCode}:edit")
    public String edit(Model model,${entity.idAttribute.javaType} ${entity.idAttribute.name}) {
        ${entity.name} ${entity.code} = ${service}.selectById(${entity.idAttribute.name});
        model.addAttribute("${entity.code}", ${entity.code});
        return "/${target.urlBase}/${entity.code}/edit";
    }

    \@GetMapping(MODEL + "/add.do")
    \@RequiresPermissions("${basicfunctionCode}:add")
    public String add() {
        return "/${target.urlBase}/${entity.code}/add";
    }

    /* ajax json */

    \@PostMapping(MODEL + "/list.json")
    \@ResponseBody
    \@RequiresPermissions("${basicfunctionCode}:query")
    public R list(${entity.name}Query condtion) {
        PageQuery<${entity.name}> page = condtion.getPageQuery();
        ${service}.queryByCondition(page);
        return R.ok().put("data",page);
    }

    \@PostMapping(MODEL + "/add.json")
    \@ResponseBody
    \@RequiresPermissions("${basicfunctionCode}:add")
    public R add(${entity.name} ${entity.code}) {
        ${service}.insert(${entity.code});
        logger.info("保存成功！");
        return R.ok("保存成功");
    }

    \@PostMapping(MODEL + "/update.json")
    \@ResponseBody
    \@RequiresPermissions("${basicfunctionCode}:update")
    public R update(${entity.name} ${entity.code}) {
        boolean success = ${service}.update(${entity.code});
        if (success) {
        	logger.info("保存成功！");
        	return R.ok("保存成功");
        } else {
        	logger.info("保存失败！");
        	return R.error("保存失败");
        }
    }


   
    \@GetMapping(MODEL + "/view.json")
    \@ResponseBody
    \@RequiresPermissions("${basicfunctionCode}:query")
    public R queryInfo(${entity.idAttribute.javaType} ${entity.idAttribute.name}) {
        ${entity.name} ${entity.code} = ${service}.selectById(${entity.idAttribute.name});
        return R.ok().put("data", ${entity.code});
    }

    \@PostMapping(MODEL + "/delete.json")
    \@ResponseBody
    \@RequiresPermissions("${basicfunctionCode}:delete")
    public R delete(String ids) {
        if (ids.endsWith(",")) {
        	ids = ids.substring(0,ids.length()-1);
        }
        String[] idList = ids.split(",");
        ${service}.batchDel${entity.name}(Arrays.asList(idList));
        return R.ok("删除成功");
    }

}
